// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    traceUser: true,
    env: 'aaa111-9gh9fp5ieac4de5d'
})

const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    var result = []
    var myTrip = await db.collection('trip').where({
        _openid: wxContext.OPENID
    }).orderBy('tripBeginTime','desc').get()

    for (var i = 0; i < myTrip.data.length; i++) {
        var temp = {
        }
        temp.title = myTrip.data[i].title
        temp.image = myTrip.data[i].image
        //
        var now = new Date(myTrip.data[i].tripBeginTime)
        var year = now.getFullYear(); //得到年份
        var month = now.getMonth() + 1; //得到月份
        var date = now.getDate(); //得到日期
        temp.beginTime = year + "年" + month + "月" + date + "日"
        //
        temp.duringDays = (myTrip.data[i].tripEndTime - myTrip.data[i].tripBeginTime) / (1 * 24 * 60 * 60 * 1000) + 1;
        temp.tripId = myTrip.data[i]._id
        result.push(temp)
    }
    return result
}